package com.dsy.xiangcun_manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dsy.tools.common.utils.AliyunOSSUtil;
import com.dsy.tools.common.utils.QueryObj;
import com.dsy.tools.entity.TbHistory;
import com.dsy.tools.entity.TbTou;
import com.dsy.xiangcun_manage.service.ITbHistoryService;
import com.dsy.xiangcun_manage.service.ITbTouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dsy
 * @since 2022-02-16
 */
@Controller
@RequestMapping("history")
public class TbHistoryController {
    @Autowired
    ITbHistoryService historyService;

    @Autowired
    ITbTouService touService;
    /**
     * 跳转到信息发布界面
     * @return
     */
    @RequestMapping("/addHistory")
    public String addHistory(){
        return "/history/history-add";
    }

    @RequestMapping("/uploadHistoryImg")
    @ResponseBody
    public Map<String,Object> uploadHistoryImg(@RequestParam("imgFile") List<MultipartFile> imgFiles){
        Map<String, Object> map = new HashMap<>();
        List<String> data = new ArrayList<>();
        for (MultipartFile imgFile : imgFiles) {
            String filename = imgFile.getOriginalFilename();
            System.out.println(filename);
            try {
                if (imgFile != null) {
                    if (!"".equals(filename.trim())) {
                        File newFile = new File(filename);
                        FileOutputStream os = new FileOutputStream(newFile);
                        os.write(imgFile.getBytes());
                        os.close();
                        imgFile.transferTo(newFile);
                        // 阿里云上传到OSS
                        String uploadUrl = AliyunOSSUtil.upLoad(newFile);
                        newFile.delete();
                        data.add(uploadUrl);
                    }
                    //
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        map.put("error",0);
        map.put("data",data);
        return map;
    }
    /**
     * 添加信息
     * @param history
     * @return
     */
    @RequestMapping("/saveHistory")
    public String saveHistory(TbHistory history, HttpSession session){
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        history.setHistoryUser(tbTou.getUserId());
        history.setHistoryTime(LocalDateTime.now());
        historyService.saveOrUpdate(history);
        return "redirect:/history/getAllMyHistory";
    }
    /**
     * 查看当前登录用户发布的
     * @param m
     * @param page
     * @param limit
     * @param queryObj
     * @return
     */
    @RequestMapping("/getAllMyHistory")
    public String getAllHistory(Model m, @RequestParam(defaultValue = "1") long page, @RequestParam(defaultValue = "10") long limit, QueryObj queryObj, HttpSession session){

        QueryWrapper<QueryObj> wrapper = new QueryWrapper<>();
        TbTou tbTou = (TbTou) session.getAttribute("tbTou");
        queryObj.setTid(tbTou.getUserId());
        wrapper.setEntity(queryObj);

        IPage<TbHistory> historyPage = historyService.getAllHistory(page,limit,wrapper);
        m.addAttribute("historyPage",historyPage);
        m.addAttribute("page",page);
        m.addAttribute("queryObj",queryObj);
        return "/history/history-base";
    }
    /**
     * 根据信息Id查询
     * @param m
     * @param historyId
     * @return
     */
    @RequestMapping("/getHistoryById")
    public String getHistoryById(Model m,Integer historyId){
        TbHistory history=historyService.getHistoryById(historyId);
        m.addAttribute("history",history);
        return "/history/history-edit";
    }

    /**
     * 修改更新信息
     * @param tbHistory
     * @return
     */
    @RequestMapping("/updateHistory")
    public String updateHistory(TbHistory tbHistory){
        tbHistory.setHistoryTime(LocalDateTime.now());
        historyService.updateById(tbHistory);
        return "redirect:/history/getAllMyHistory";
    }

    /**
     * 删除信息
     * @param historyIds
     * @return
     */
    @RequestMapping("/delHistoryById")
    public String delAroundById(@RequestParam("historyId") List<Integer> historyIds){
        historyService.removeByIds(historyIds);
        return "redirect:/history/getAllMyHistory";
    }


}
